// 作为 iframe 嵌入页面通过 postmessage  通信
const event_word_receive_namespace = 'HUSSAR_WORD_EVENT_RECEIVE';
const event_word_send_namespace = 'HUSSAR_WORD_EVENT_SEND';
const search = window.location.search;
const urlParams = new URLSearchParams(search);

export const getQueryString = (name) => {
  const value = urlParams.get(name);
  return decodeURIComponent(value);
};
function handleMessage(event, callback) {
  if (event.data[event_word_receive_namespace]) {
    console.log('iframe = ', event);
    // {
    //     type:'openWord',
    //     data:{
    //         docId:'1'
    //     }
    // }
    callback(event.data[event_word_receive_namespace]);
  }
}
export const onMessage = (callback) => {
  window.addEventListener('message', (event) => handleMessage(event, callback));
};

export const emitMessage = (message) => {
  window.parent.postMessage({ [event_word_send_namespace]: message }, '*');
};

export const removeEventListener = () => {
  window.removeEventListener('message', handleMessage);
};
